package com.net.oil.action.model;

import com.net.oil.action.bean.ActionDetailBean;
import com.net.oil.action.contract.ActionDetailContract;
import com.net.oil.action.presenter.ActionDetailPresenter;
import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.AppHttpCode;
import com.net.oil.http.custom.ApiException;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/5/17 0017:下午 21:08
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ActionDetailModel extends BaseModel implements ActionDetailContract.Model {

    private ActionDetailPresenter presenter;

    public ActionDetailModel(ActionDetailPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getActionDetail(int actionId) {
        AppHttpClient.ApiService().getActionDetail(actionId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ActionDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<ActionDetailBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getActionDetailSuccess(tResultBean.getData());
                        }else {
                            presenter.getActionDetailFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getActionDetailFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void getCoupon(int actionId) {
        AppHttpClient.ApiService().getCoupon(actionId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.getCouponSuccess();
                        }else {
                            presenter.getCouponFailed(new ApiException(AppHttpCode.ERROR, "没有数据"));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ApiException exception = (ApiException) e;
                        presenter.getCouponFailed(exception);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
